package com.design.oop;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.design.oop.bean.WheatBeerKind;
import com.design.oop.bean.WheatBeerKind.SortType;
import com.design.oop.util.CommonUtil;
import com.design.oop.util.FileUtil;
import com.design.oop.util.ImageUtil;

public class WheatBeerInitActivity extends Activity {
	static final String ASSET_DIR = "image";

	AlertDialog infoDialog = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.init);

		String[] fileList = null;
		try {
			fileList = WheatBeerApplication.getContext().getAssets().list(ASSET_DIR);
		} catch (IOException e1) {
		}

		for (int i = 0; i < fileList.length; i++) {
			String file = fileList[i];
			try {
				readImage(file);
			} catch (IOException e) {
			}
		}
	}

	private void readImage(String filename) throws IOException {
		BufferedInputStream bis = null;
		FileOutputStream fos = null;

		File file = new File(FileUtil.getImageDir() + "/" + filename);
		FileUtil.delete(file);

		try {
			bis = new BufferedInputStream(WheatBeerApplication.getContext().getAssets().open(ASSET_DIR + "/" + filename));
			fos = new FileOutputStream(file);

			byte[] buf = new byte[1024];
			int nread;

			while ((nread = bis.read(buf)) != -1) {
				fos.write(buf, 0, nread);
			}
		} catch (Exception ex) {
		} finally {
			CommonUtil.closeQuietly(bis);
			CommonUtil.closeQuietly(fos);
		}

		int[] realImageSize = ImageUtil.getBitmapSize(file.getAbsolutePath());
		WheatBeerKind beer = WheatBeerKind.getBeerByFileName(filename);
		beer.setImageUrl(file.getAbsolutePath());
		beer.setWidth(realImageSize[0]);
		beer.setHeight(realImageSize[1]);
	}

	public void onClickedBeerKind(View view) {
		Intent intent = new Intent(this, WheatBeerKindActivity.class);
		startActivity(intent);
	}

	public void onClickedAllBeerKind(View view) {
		Intent newIntent = new Intent(this, WheatBeerListActivity.class);
		startActivity(newIntent);
	}

	public void onClickedRate(View view) {
		Intent intent = new Intent(this, WheatBeerKindActivity.class);
		intent.putExtra("sortType", SortType.RATE.getValue());
		startActivity(intent);
	}

	public void onClickedDegree(View view) {
		Intent intent = new Intent(this, WheatBeerKindActivity.class);
		intent.putExtra("sortType", SortType.DEGREE.getValue());
		startActivity(intent);
	}

	public void onClickedRandom(View view) {
		Intent intent = new Intent(this, WheatBeerKindActivity.class);
		int index = new Random().nextInt(WheatBeerKind.BEER_LIST.size());
		intent = new Intent(WheatBeerInitActivity.this, WheatBeerDetailActivity_.class);
		intent.putExtra("id", WheatBeerKind.BEER_LIST.get(index).getDrawableId());
		startActivity(intent);
	}

	public void onClickedBookmark(View view) {
		Intent intent = new Intent(this, WheatBeerKindActivity.class);
		intent.putExtra("bookmark", true);
		startActivity(intent);
	}

	public void onClickedLogo(View view) {
		LayoutInflater factory = LayoutInflater.from(this);
		LinearLayout dialogView = (LinearLayout)factory.inflate(R.layout.stat_dialog, null);
		TextView statDialogTitle = (TextView)dialogView.findViewById(R.id.stat_dialog_title);
		statDialogTitle.setText("만든이들");

		TextView thisMonthResult = (TextView)dialogView.findViewById(R.id.this_month_result_text);
		thisMonthResult.setText("밀맥주앱을 살아숨쉬게 만들어준 ㄱㅅㄱ님께 깊은 감사를 드립니다.");

		ImageView closeBtn = (ImageView)dialogView.findViewById(R.id.stat_dialog_close_btn);
		closeBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (infoDialog != null) {
					infoDialog.dismiss();
					infoDialog = null;
				}
			}
		});

		AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(WheatBeerInitActivity.this, R.style.alertCustomTheme));
		builder.setView(dialogView);
		infoDialog = builder.show();
	}
}